<?php
//App::uses('AppController', 'Controller');

class NotifiController extends AppController
{
    public $name = 'Notifi';
	public $uses = array('tbl_log', 'MaterialInfoList','CostumeinfoList');

	public function all() {

        if(!empty($this->data['notice'])) {
            $msg = $this->data['notice']['msg'];
            if(!empty($msg)) {
                App::uses('HttpSocket', 'Network/Http');
                $HttpSocket = new HttpSocket();

                $url = $this->server_url . '/notice_all';
                $data = 'msg='.$msg;

                $result = json_decode($HttpSocket->post($url, $data), true);

                if($result['rcode'] == 0) {
                    $this->Session->setFlash('公告发送成功', 'default', array('class' => 'success'), 'good');
                    $result_info = '公告发送成功';
                } else {
                    $this->Session->setFlash('公告发送失败,请检查网络.', 'default', array('class' => 'message'), 'bad');
                    $result_info = '公告发送失败!!';
                }

                //로그 설정
                $logdata = array("client_ip"=>$_SERVER['REMOTE_ADDR'],"date:"=>date('Y-m-d H:i:s',time()),'operation_info'=>$url,'operation_data'=>$data,'operation_result'=>$result_info);
                $this->tbl_log->save($logdata);
            }
        }

        $this->set(compact(array()));
	}

    public function send_all_mail()
    {
        if(!empty($this->data['mailAll'])) {
            $msg = $this->data['mailAll']['send_all_mail'];
            if(!empty($msg)) {
                $data = array('message'=>$msg);

                $result_data = $data ;

                $event_url = $this->server_url. '/mail_all';
                App::uses('HttpSocket', 'Network/Http');
                $HttpSocket = new HttpSocket();
                $result = json_decode($HttpSocket->post($event_url, $data), true);

                $result_data = $data ;

                $result_code = $result['rcode'] ;
                @$result_msg = $result['message'] ;
                if($result['rcode'] == 0) {
                    $result_info = '邮件发送成功' ;
                } else {
                    $result_info = '邮件发送失败.' ;
                }

                //로그 설정
                $logdata = array("client_ip"=>$_SERVER['REMOTE_ADDR'],"date:"=>date('Y-m-d H:i:s',time()),'operation_info'=>$event_url,'operation_data'=>$data,'operation_result'=>$result_info);
                $this->tbl_log->save($logdata);

                $this->set(compact('result_code', 'result_msg','result_info', 'result_data'));
            }
        }
    }

    public function send_all_item()
    {
        $send_item_info = $this->data['sendall_item_info'];
        $event_url = '';
        $item_cnt =  $send_item_info ['count_all'];
        $evnet_message = $send_item_info ['message_all'];

        $reward_id = $send_item_info['reward_id_all'];
        $reward_type = $send_item_info['reward_type_all'];

        $costume_id = $send_item_info['costume_id_all'];
        $expire_time = $send_item_info['expire_time_all'];

        $result_info = '';
        $result_code = '';
        $result_msg = '' ;
        $result_info = '';

        $data = '';

        if( empty($evnet_message) ) {
            $result_info = '--------------请输入消息!!!!!!!!!!!!-----------------';
            $this->set(compact('result_code', 'result_msg','result_info', 'result_data'));
            return;
        }

        if( $reward_type == 'Material' ) {
            if( empty($reward_id) ) {
                $result_info = '--------------请选择재료!!!!!!!!!!!!-----------------';
                $this->set(compact('result_code', 'result_msg','result_info', 'result_data'));
                return;
            }
            $expire_time = 0;
        }
        else if ($reward_type == 'Costume'){
            if( empty($costume_id) ) {
                $result_info = '--------------请选择costume种类!!!!!!!!!!!!-----------------';
                $this->set(compact('result_code', 'result_msg','result_info', 'result_data'));
                return;
            }
            $reward_id = $costume_id ;
        }
        else {
            $reward_id = $reward_type;
            $expire_time = 0 ;
        }

        if ($reward_type != 'Costume') {
            if ($item_cnt == '' || $item_cnt <= 0) {
                $this->Session->setFlash('请输入수량.', 'default', array('class' => 'message'), 'bad');
                return;
            }
        }
        else{
            if (empty($expire_time)){
                $this->Session->setFlash('请填入costume지속시간.', 'default', array('class' => 'message'), 'bad');
                return;
            }
            $item_cnt = 1 ;
        }

        $data = array('reward_id'=>$reward_id,'reward_type'=>$reward_type,'message'=>$evnet_message,'count'=>$item_cnt,'expire_hour'=>$expire_time);
        //print_r($data);
        $result_data = $data ;

        $event_url = $this->server_url. '/notification_all';
        App::uses('HttpSocket', 'Network/Http');
        $HttpSocket = new HttpSocket();
        $result = json_decode($HttpSocket->post($event_url, $data), true);

        $result_data = $data ;

        $result_code = $result['rcode'] ;
        @$result_msg = $result['message'] ;
        if($result['rcode'] == 0) {
            $result_info = '아이템发送成功' ;
        } else {
            $result_info = '아이템发送失败.' ;
        }

        //로그 설정
        $logdata = array("client_ip"=>$_SERVER['REMOTE_ADDR'],"date:"=>date('Y-m-d H:i:s',time()),'operation_info'=>$event_url,'operation_data'=>$data,'operation_result'=>$result_info);
        $this->tbl_log->save($logdata);

        $this->set(compact('result_code', 'result_msg','result_info', 'result_data'));
    }

    public function multi_reward() {
        $ticketId = $this->__getMaterialId();
        $this->set(compact('ticketId'));

        $costumeId = $this->__getCostumeId();
        $this->set(compact('costumeId'));

        if( !empty($this->data) ) {
            $data = $this->data['multi_reward'];
            $nick_name_array = explode(',', $data['nick_name']);
            $reward_id = $data['reward_id'];
            $reward_type = $data['reward_type'];
            $count = $data['count'];
            $message = $data['message'];

            $costume_id = $data['costume_id'];
            $expire_time = $data['expire_time'];

            if( empty($nick_name_array) ) {
                $this->Session->setFlash('请输入角色名.', 'default', array('class' => 'message'), 'bad');
                return;
            }

            if( empty($message) ) {
                $this->Session->setFlash('请输入消息.', 'default', array('class' => 'message'), 'bad');
                return;
            }

            if( $reward_type == 'Material' ) {
                if( empty($reward_id) ) {
                    $this->Session->setFlash('请选择재료种类.', 'default', array('class' => 'message'), 'bad');
                    return;
                }
                $expire_time = 0;
            }
            else if ($reward_type == 'Costume'){
                if( empty($costume_id) ) {
                    $this->Session->setFlash('请选择costume种类.', 'default', array('class' => 'message'), 'bad');
                    return;
                }
                $reward_id = $costume_id ;
            }
            else {
                $reward_id = $reward_type;
                $expire_time = 0 ;
            }

            if ($reward_type != 'Costume') {
                if ($count == '' || $count <= 0) {
                    $this->Session->setFlash('请输入수량.', 'default', array('class' => 'message'), 'bad');
                    return;
                }
            }
            else{
                if (empty($expire_time)){
                    $this->Session->setFlash('请填入costume지속시간.', 'default', array('class' => 'message'), 'bad');
                    return;
                }
                $count = 1 ;
            }

            $nick_name_result = array();
            foreach( $nick_name_array as $value ) {
                if(!empty($value))
                    $nick_name_result[] = $value;
            }
            
            $nick_name = implode(",", $nick_name_result);

            if( !empty($nick_name) ) {
                App::uses('HttpSocket', 'Network/Http');
                $HttpSocket = new HttpSocket();

                $url = $this->server_url . '/multi_user_reward';
                $request = array(
                    'nick_name' => $nick_name,
                    'reward_id' => $reward_id,
                    'reward_type' => $reward_type,
                    'count' => $count,
                    'message' => $message,
                    'expire_hour' => $expire_time
                );

                $result = json_decode($HttpSocket->post($url, $request), true);
                if( empty($result) ) {
                    $this->Session->setFlash('服务器失去连接.', 'default', array('class' => 'message'), 'bad');
                    return;
                }

                if( $result['rcode'] != 0 ) {
                    $this->Session->setFlash('错误代码 : '.$result['rcode'], 'default', array('class' => 'message'), 'bad');
                    return;
                }

                if( count($result['data']['empty']) > 0 ) {
                    $empty_nick_name = implode(",", $result['data']['empty']);
                    $this->Session->setFlash('没有相应的角色名 : '.$empty_nick_name, 'default', array('class' => 'message'), 'bad');
                }

                if( count($result['data']['fail']) > 0 ) {
                    $fail_nick_name = implode(",", $result['data']['fail']);
                    $this->Session->setFlash('失败 角色名 : '.$fail_nick_name, 'default', array('class' => 'message'), 'bad2');
                }

                if(count($result['data']['success']) > 0)
                    $this->Session->setFlash('奖励发送完成 : '.count($result['data']['success']).'名', 'default', array('class' => 'success'), 'good');
            }
        }
    }

    private function __getMaterialId()
    {
        $data = $this->MaterialInfoList->getAllId();
        $dataId = array(
            '' => '선택안함'
        );
        foreach($data as $value)
        {
            $dataId[$value['MaterialInfoList']['Id']] = $value['MaterialInfoList']['Name'];
        }

        return $dataId;
    }

    private function __getCostumeId()
    {
        $data = $this->CostumeinfoList->getAllId();
        $dataId = array(
            '' => '선택안함'
        );
        foreach($data as $value)
        {
            $dataId[$value['CostumeinfoList']['Id']] = $value['CostumeinfoList']['Name'];
        }

        return $dataId;
    }
}
